package com.ruoyi.merchant.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.merchant.service.ShopRepository;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.domain.Shop;
import com.ruoyi.merchant.service.IShopService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 店铺：存储店铺的基本信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/shop")
public class ShopController extends BaseController
{
    @Autowired
    private IShopService shopService;

    //搜索记录
    @GetMapping("/search")
    public AjaxResult searchShops(@RequestParam(required = false) String name) {
        Long userId = SecurityUtils.getUserId();

        List<Shop> shops = shopService.searchShopsByName(name);
        // 保存搜索历史
        shopService.saveSearchHistory(userId, name);
        return success(shops);
    }

    //商铺数据同步
    @PostMapping("/sync")
    public AjaxResult syncShops() {
        shopService.syncShopsToEs();
        return success("数据已同步到 Elasticsearch");
    }

    @GetMapping("/history")
    public AjaxResult getHistory(HttpServletRequest request) {
        Long userId = SecurityUtils.getUserId();
        System.out.println("用户的Id为:" + userId);
        List<Object> history = shopService.getSearchHistory(userId);
        return AjaxResult.success(history);
    }

    /**
     * 查询店铺：存储店铺的基本信息列表
     */
    @RequiresPermissions("merchant:shop:list")
    @GetMapping("/list")
    public TableDataInfo list(Shop shop)
    {
        startPage();
        List<Shop> list = shopService.selectShopList(shop);
        return getDataTable(list);
    }

    /**
     * 导出店铺：存储店铺的基本信息列表
     */
    @RequiresPermissions("merchant:shop:export")
    @Log(title = "店铺：存储店铺的基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Shop shop)
    {
        List<Shop> list = shopService.selectShopList(shop);
        ExcelUtil<Shop> util = new ExcelUtil<Shop>(Shop.class);
        util.exportExcel(response, list, "店铺：存储店铺的基本信息数据");
    }

    /**
     * 获取店铺：存储店铺的基本信息详细信息
     */
    @RequiresPermissions("merchant:shop:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(shopService.selectShopById(id));
    }

    @GetMapping(value = "/type/{typeId}")
    public AjaxResult getInfoByType(@PathVariable("typeId") Integer typeId,
                                    @RequestParam(defaultValue = "30") Double radiusKm) {
        // 获取所有店铺的经纬度
        List<Shop> shops = shopService.selectShopList(null);

        shopService.saveShopLocation(shops);

        Long userId = SecurityUtils.getUserId();
        return success(shopService.selectShopByType(typeId, userId, radiusKm));
    }

    /**
     * 新增店铺：存储店铺的基本信息
     */
    @RequiresPermissions("merchant:shop:add")
    @Log(title = "店铺：存储店铺的基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Shop shop)
    {
        return toAjax(shopService.insertShop(shop));
    }

    /**
     * 修改店铺：存储店铺的基本信息
     */
    @RequiresPermissions("merchant:shop:edit")
    @Log(title = "店铺：存储店铺的基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Shop shop)
    {
        return toAjax(shopService.updateShop(shop));
    }

    /**
     * 删除店铺：存储店铺的基本信息
     */
    @RequiresPermissions("merchant:shop:remove")
    @Log(title = "店铺：存储店铺的基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(shopService.deleteShopByIds(ids));
    }


    @InnerAuth
    @PostMapping("/register1")
    public Integer register1(@RequestBody Shop shop){
        return shopService.addShop(shop);
    }

    // 获取店铺的信息
    @GetMapping("/shopInfo")
    public AjaxResult getShopInfo(HttpServletRequest request) {
        LoginUser user = SecurityUtils.getLoginUser();
        System.out.println( user);
        String userName = user.getSysUser().getUserName();
        Shop shopInfo = shopService.getShopInfo(userName);
        return success(shopInfo);
    }
}
